// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).
using System;
using System.Runtime.Serialization;

namespace Tierless.Framework.Constraints.Common
{
	/// <summary>
	/// Enforces that a column has a value, in particular that strings are non-null and non-empty.
	/// </summary>
	[DataContract]
	public class NotNullOrEmpty : ColumnConstraint
	{
		/// <summary>
		/// Constructs a new instance of the NotNullOrEmpty constraint.
		/// </summary>
		/// <param name="entityTypeName">The name of the entity type on which the constraint will be placed.</param>
		/// <param name="columnName">The name of the column on which the constraint will be placed.</param>
		public NotNullOrEmpty(string entityTypeName, string columnName)
			: base
			(
				String.Format("{0}_{1}NotNullOrEmpty", entityTypeName, columnName),
				entityTypeName,
				columnName,
				String.Format("A value is required for {0}.", columnName)
			)
		{}

		/// <summary>
		/// Constructs a new instance of the NotNullOrEmpty constraint.
		/// </summary>
		/// <param name="entityTypeName">The name of the entity on which the constraint will be placed.</param>
		/// <param name="columnName">The name of the column on which the constraint will be placed.</param>
		/// <param name="columnDescription">The description of the column used to construct the violation message for the constraint.</param>
		public NotNullOrEmpty(string entityTypeName, string columnName, string columnDescription)
			: base
			(
				String.Format("{0}_{1}NotNullOrEmpty", entityTypeName, columnName),
				entityTypeName,
				columnName,
				String.Format("A value is required for {0}.", columnDescription)
			)
		{}

		protected override bool IsValid(ConstraintContext context)
		{
			return !context.EntityChange.Entity[ColumnName].IsEmpty;
		}
	}
}